package cn.edu.zjx.service.impl;

import com.management.entity.Department;
import com.management.mapper.DepartmentMapper;
import com.management.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    
    @Autowired
    private DepartmentMapper departmentMapper;
    
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.selectAll();
    }
    
    @Override
    public Department getDepartmentById(Integer id) {
        return departmentMapper.selectById(id);
    }
    
    @Override
    @Transactional
    public boolean addDepartment(Department department) {
        try {
            return departmentMapper.insert(department) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    @Transactional
    public boolean updateDepartment(Department department) {
        try {
            return departmentMapper.update(department) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    @Transactional
    public boolean deleteDepartment(Integer id) {
        try {
            return departmentMapper.deleteById(id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public List<Department> searchDepartments(String keyword) {
        return departmentMapper.search(keyword);
    }
} 